//*******************************************************************************
//**************** Populate building base on selectionned camp ******************
//*******************************************************************************

_position = _this select 0;

//Vars
_areasize = radius; //Size of the brush where to fill buildings
_spawnpercent = _this select 1;//% of the chance to spawn an unit at a building position

//Lets pick our arrays of units first
_units=[];

//Select camp
call compile format["_units=%1_Man;",var_console_valid_camp];

if (isnil "_units" or count _units<1) exitwith {hint "!!! no men for this camp !!!"};

_unitsnum=count _units;

if (var_console_valid_side=="object") exitwith {hint "!!! Invalid side / army selectionned !!!";};

//Let's count how much houses can be filled with freash meat and create a fillable array
_validhouses=[];
_houses = nearestObjects [[_position select 0, _position select 1], ["Building"], _areasize];
_housenum = count _houses;

for "_x" from 0 to _housenum do
{
  _currenthouse=_houses select _x;
  _n=_currenthouse call buildingPosCount; 
  if (_n>0) then {_validhouses set [ count _validhouses, _currenthouse]};
};

//Now i know my fillable houses, let's the fun begin

//First, we create a group faction from the selected side 
_group = call compile format["createGroup %1",var_console_valid_side];
_numvalidhouse=count _validhouses;
//_group = createGroup east;
//      hint format["%1",_numvalidhouse];

if (_numvalidhouse<1) exitwith {hint "!!! No building with interior detected in the area !!!";};

//How much spawn are we gonna get ?
_spawned=0;

//Let's fill house one by one
for "_x" from 0 to _numvalidhouse do
{
  _currenthouse=_validhouses select _x;
  _countpos=_currenthouse call buildingPosCount;
  _loop=0;
  while {_loop<=_countpos} do
  {
    if (round(random 100)>=(100-_spawnpercent)) then
    {
      _unittospawn=_units select (round(random(_unitsnum-1)));
      if (([_unittospawn,"pilot"] call KRON_StrInStr) or ([_unittospawn,"crew"] call KRON_StrInStr)) then
      {
        _unittospawn=_units select 0;
      };
      _buildingpos=_currenthouse buildingPos _loop;
      if (count (nearestObjects [_buildingpos, ["Man"], 1])<1 ) then
      {
        _unit =_group createUnit [_unittospawn,_buildingpos,[],0,"NONE"];
        //_unit disableAI "MOVE";
        //_unit disableAI "TARGET";
        doStop _unit;
        _unit setUnitPosWeak "UP";
        _unit setUnitPos "UP";
        
        _unit setpos _buildingpos;
        _unit setSkill console_unit_moral;
        _unit setDir (random 360);
         
        _marker_test = createMarkerLocal ["bmarker",_buildingpos];
    		_marker_test setMarkerShapeLocal "ELLIPSE";
    		_marker_test setMarkerSizeLocal [2, 2];
    		_marker_test setMarkerDirLocal 0;
    		_marker_test setMarkerColorLocal "ColorredAlpha";
    		 sleep 0.1;
    		deletemarker "bmarker";
    		
        _spawned=_spawned+1;
      };
    };
    _loop=_loop+1;
  };
   
   _group setBehaviour "AWARE";
   _group setCombatMode "YELLOW";
  
};
_code=nil;
call compile format["
_code={
if (player==user1) then {hint ""Spawn result : %1 unit(s) in %2 building(s)"";};
};
",_spawned,_numvalidhouse];
[_code] execvm "scripts\VTS_Functions\runcodeonallclients.sqf";
